package sogo.myi.micro.sh.test;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

/**
 * 分类下数据均匀分配获取
 * 
 * @author zhengpei
 *
 */
public class CatBalanceAllocate2List {

	public static void main(String[] args) {
		Cat cat0 = new Cat();
		List<String> data0s = new ArrayList<>();
		cat0.setDatas(data0s);
		data0s.add("A0");
		data0s.add("A1");
		data0s.add("A2");
		data0s.add("A3");
		data0s.add("A4");
		data0s.add("A5");
		cat0.setDataSize(data0s.size());
		cat0.setName("AAA");

		Cat cat1 = new Cat();
		List<String> data1s = new ArrayList<>();
		cat1.setDatas(data1s);
		data1s.add("B0");
		data1s.add("B1");
		data1s.add("B2");
		data1s.add("B3");
		data1s.add("B4");
		data1s.add("B5");
		data1s.add("B6");
		data1s.add("B7");
		data1s.add("B8");
		data1s.add("B9");
		cat1.setDataSize(data1s.size());
		cat1.setName("BBB");

		Cat cat2 = new Cat();
		List<String> data2s = new ArrayList<>();
		cat2.setDatas(data2s);
		data2s.add("C0");
		data2s.add("C1");
		data2s.add("C2");
		data2s.add("C3");
		data2s.add("C4");
		data2s.add("C5");
		data2s.add("C6");
		data2s.add("C7");
		data2s.add("C8");
		data2s.add("C9");
		data2s.add("C10");
		data2s.add("C11");
		data2s.add("C12");
		data2s.add("C13");
		data2s.add("C14");
		cat2.setDataSize(data2s.size());
		cat2.setName("CCC");

		cat0.setNext(cat1);
		cat1.setNext(cat2);
		cat2.setNext(cat0);

		List<String> result = new ArrayList<>();
		CatManager catManager = new CatManager();
		catManager.setCurrentCat(cat0);
		catManager.setCatCnt(3);
		while (catManager.hasNextData) {
			String nextData = catManager.getNextData();
			if (StringUtils.isNotBlank(nextData))
				result.add(nextData);
		}
		for (String str : result) {
			System.out.println(str);
		}
	}

	/**
	 * 分类管理器
	 * 
	 * @author zhengpei
	 *
	 */
	public static final class CatManager {

		private Cat currentCat;

		/**
		 * 分类数量
		 */
		private int catCnt;

		private boolean hasNextData = true;

		/**
		 * 分类下数据取完的分类数量
		 */
		private int catEmptyCnt;

		public Cat getNextCat() {
			return currentCat.getNext();
		}

		public boolean hasNextData() {
			return hasNextData;
		}

		public String getNextData() {
			String data = currentCat.next();
			if (StringUtils.isBlank(data)) {
				catEmptyCnt++;
			} else {
				// catEmptyCnt clear
				catEmptyCnt = 0;
			}
			// 所有分类数据获取完
			if (catEmptyCnt == catCnt) {
				hasNextData = false;
			}
			currentCat = currentCat.getNext();
			return data;
		}

		public void setCurrentCat(Cat currentCat) {
			this.currentCat = currentCat;
		}

		public void setCatCnt(int catCnt) {
			this.catCnt = catCnt;
		}

	}

	public static final class Cat {

		/**
		 * 分类名称
		 */
		private String name;
		/**
		 * 下个分类
		 */
		private Cat next;

		/**
		 * data集合大小
		 */
		private int dataSize;

		/**
		 * 下一次获取data索引
		 */
		private int dataIndex;
		/**
		 * 分类数据
		 */
		private List<String> datas;

		/**
		 * 获取下个索引数据
		 * 
		 * @return
		 */
		public String next() {
			if (dataIndex >= dataSize) {
				return null;
			}
			String data = datas.get(dataIndex);
			dataIndex++;
			return data;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public Cat getNext() {
			return next;
		}

		public void setNext(Cat next) {
			this.next = next;
		}

		public void setDatas(List<String> datas) {
			this.datas = datas;
		}

		public void setDataSize(int dataSize) {
			this.dataSize = dataSize;
		}

	}
}
